<?php
include "connection.php";

session_start();
$order_user = $_SESSION['userid'];
date_default_timezone_set('prc');


$orderID = $_POST['orderID'];
$productID = $_POST['productID'];
$orderPrice = $_POST['orderPrice'];
$productDate = $_POST['productDate'];

$temp = str_replace("年","",$productDate);
$temp1 = str_replace("月","",$temp);
$temp2 = str_replace("日","",$temp1);
$temp3 = strtotime($temp2);

$nowDate = date('Ymd H:i:s',time());
$nowDate_int=strtotime($nowDate)+23400;

if($nowDate_int<$temp3){



    $tableOrder = "orderlist";



    $sqlstr = "select count(nid) from $tableOrder where (nid='$orderID' and product_id='$productID' and user_id='$order_user')";
    $query = mysql_query($sqlstr);
    $sum = mysql_result($query, 0);



    if($sum){

        $sqldelete = "delete from $tableOrder where (nid='$orderID' and product_id='$productID' and user_id='$order_user')";


        $querydelete=mysql_query($sqldelete) or die ('删除数据失败');

        $tableUser = "user";

        $result = mysql_query("select * from $tableUser where user_id ='$order_user'");

        while ($row = mysql_fetch_array($result)) {
            $balance = $row['balance'];
        }

        $newBalance = $balance + $orderPrice;

        $sql = "update $tableUser set balance = '$newBalance' where user_id = '$order_user'";
        $query = mysql_query($sql) or die ('退款失败！');

        echo "退订已成功，请重新查询！";
    }else{
        echo "当天订单、过期订单不能退订！超过17:00不能退订明天订单！";
    }

}




?>